/**
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#if DEBUG_VERSION
if (!ark) {
  throw new Error("ARK namespace not found!");
}
#endif

ark.InputDisplay = function(parent) {
  this.parent_ = parent;
  this.className_ = "InputDisplay";
};

ark.InputDisplay.prototype.setClass = function(className) {
  this.className_ = className;
};

ark.InputDisplay.prototype.onOk = function(callback) {
  this.hide();
  callback(this.input_.value);
};

ark.InputDisplay.prototype.onCancel = function(callback) {
  this.hide();
  callback();
};

ark.InputDisplay.prototype.onKeyUp = function(callback, event) {
  event = event || window.event;
  var key = event.keyCode || event.which;
  if (key == 13) {
    this.onOk(callback);
  }
};

ark.InputDisplay.prototype.show = function(labelText, onClose) {
  this.box_ = w23.e("div", { className : this.className_ });
  var label = w23.t(labelText);
  this.input_ = w23.e("input", 
	  { onkeyup : ark.bind(this.onKeyUp, this, onClose) });
  var okbutton = w23.e(
      "button", 
      { onclick : ark.bind(this.onOk, this, onClose) }, 
      "Ok");
  var cancelbutton = w23.e(
      "button", 
      { onclick : ark.bind(this.onCancel, this, onClose) }, 
      "Cancel");
  var table = 
      w23.e("table", null,
          w23.e("tbody", null, 
              w23.e("tr", null,
                  w23.e("td", { "colspan" : 3 }, label)
              ),
              w23.e("tr", null,
                  w23.e("td", null, this.input_),
                  w23.e("td", null, okbutton),
                  w23.e("td", null, cancelbutton)
              )
          )
      );

  w23.append(this.box_, table);
  w23.append(this.parent_, this.box_);
  this.input_.focus();
};

ark.InputDisplay.prototype.hide = function() {
  this.parent_.removeChild(this.box_);
  this.box_ = null;
};


